"""
输入框组件测试
"""
import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QLabel, QHBoxLayout
from PyQt5.QtCore import Qt

from component.input import Input
from component.button import Button
from component.theme import Theme

class TestWindow(QMainWindow):
    """测试窗口"""
    
    def __init__(self):
        super().__init__()
        
        # 设置窗口属性
        self.setWindowTitle("输入框组件测试")
        self.resize(500, 400)
        
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建布局
        layout = QVBoxLayout(central_widget)
        layout.setContentsMargins(20, 20, 20, 20)
        layout.setSpacing(20)
        
        # 添加标题
        title = QLabel("输入框组件测试")
        title.setAlignment(Qt.AlignCenter)
        title.setStyleSheet("font-size: 18px; font-weight: bold;")
        layout.addWidget(title)
        
        # 添加说明
        description = QLabel("测试输入框组件的清除按钮功能")
        description.setAlignment(Qt.AlignCenter)
        layout.addWidget(description)
        
        # 添加普通输入框
        self.input1 = Input(label="普通输入框", placeholder="请输入内容")
        layout.addWidget(self.input1)
        
        # 添加密码输入框
        self.input2 = Input(label="密码输入框", placeholder="请输入密码", password_mode=True)
        layout.addWidget(self.input2)
        
        # 添加带按钮的输入框
        search_container = QWidget()
        search_layout = QHBoxLayout(search_container)
        search_layout.setContentsMargins(0, 0, 0, 0)
        search_layout.setSpacing(10)
        
        self.input3 = Input(label="搜索输入框", placeholder="请输入搜索内容")
        search_button = Button("搜索", button_type="primary")
        
        search_layout.addWidget(self.input3)
        search_layout.addWidget(search_button)
        
        layout.addWidget(search_container)
        
        # 添加显示标签
        self.display_label = QLabel("输入内容将显示在这里")
        self.display_label.setAlignment(Qt.AlignCenter)
        self.display_label.setStyleSheet("margin-top: 20px; padding: 10px; background-color: #f5f5f5; border-radius: 4px;")
        layout.addWidget(self.display_label)
        
        # 连接信号
        self.input1.textChanged.connect(self.on_text_changed)
        self.input2.textChanged.connect(self.on_text_changed)
        self.input3.textChanged.connect(self.on_text_changed)
        
        # 添加按钮
        button_container = QWidget()
        button_layout = QHBoxLayout(button_container)
        button_layout.setContentsMargins(0, 0, 0, 0)
        button_layout.setSpacing(10)
        
        clear_all_button = Button("清除所有", button_type="primary")
        clear_all_button.clicked.connect(self.clear_all)
        
        button_layout.addStretch()
        button_layout.addWidget(clear_all_button)
        
        layout.addWidget(button_container)
        layout.addStretch()
    
    def on_text_changed(self, text):
        """文本变化处理"""
        sender = self.sender()
        if sender == self.input1:
            self.display_label.setText(f"普通输入框: {text}")
        elif sender == self.input2:
            self.display_label.setText(f"密码输入框: {text}")
        elif sender == self.input3:
            self.display_label.setText(f"搜索输入框: {text}")
    
    def clear_all(self):
        """清除所有输入框"""
        self.input1.clear()
        self.input2.clear()
        self.input3.clear()
        self.display_label.setText("所有输入框已清除")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    
    # 设置应用样式
    app.setStyleSheet(Theme().get_style_sheet())
    
    window = TestWindow()
    window.show()
    
    sys.exit(app.exec_()) 